/**
 * 
 */

package com.shy.portal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.shy.base.dao.impl.BaseDaoImpl;
import com.shy.base.vo.EntityTypeValue;
import com.shy.base.vo.PagerFilter;
import com.shy.base.vo.QueryResult;
import com.shy.portal.dao.PermissionDao;
import com.shy.portal.entity.Permission;

/**
 * <p>
 * Title: PermissionDaoImpl
 * </p>
 * <p>
 * Description:权限管理dao层实现
 * </p>
 * <p>
 * Company:ipebg
 * </p>
 * 
 * @author H2013788
 */
@Repository("permissionDao")
public class PermissionDaoImpl extends BaseDaoImpl<Permission> implements PermissionDao {

    @Override
    public PagerFilter listPermissionByPager(String whereHql, List<EntityTypeValue> params,
            PagerFilter pagerFilter) {
        Integer firstStart = Integer.parseInt(String.valueOf(pagerFilter.getStart())); // 分页开始下标
        Integer showNum = Integer.parseInt(String.valueOf(pagerFilter.getMax())); // 显示条数
        QueryResult<Permission> result = this.getScrollDataBindName(firstStart, showNum, whereHql,
                params, pagerFilter.getOrderby());
        if (result != null) {
            pagerFilter.setTotal(result.getTotalrecord());
            pagerFilter.setObjs(result.getResultlist());
        }
        return pagerFilter;
    }

    @Override
    public int getMaxLevel() {
        String sql = " select Max(level) from Permission ";
        Query query = entityManager.createQuery(sql);
        Object o = query.getSingleResult();;
        if (o != null && !"".equals(o.toString())) {
            return Integer.parseInt(o.toString());
        }
        return 0;
    }

    @SuppressWarnings("unchecked")
	@Override
    public List<Permission> getPermissionsByRoleId(Integer roleId) {
        String hql = " FROM Permission p WHERE p.id IN ( SELECT DISTINCT permission.id FROM RolePermission WHERE role.id =:roleId ) ORDER BY p.sort ASC ";
        Query query = entityManager.createQuery(hql);
        query.setParameter("roleId", roleId);
        List<Permission> list = query.getResultList();
        return list;
    }

}
